package com.leetcode;

import com.leetcode.common.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 257. 二叉树的所有路径
 * 由底到顶的递归
 *
 * @author fy
 * @date 2022-04-23 19:45
 */
public class Solution257 {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        if (root.left == null && root.right == null) {
            res.add(String.valueOf(root.val));
            return res;
        }
        // root.left 或者 root.right 为空不影响，返回的是一个空list
        List<String> leftList = binaryTreePaths(root.left);
        List<String> rightList = binaryTreePaths(root.right);

        leftList.addAll(rightList);

        for (String s : leftList) {
            res.add(root.val + "->" + s);
        }
        return res;
    }

}
